<!-- sample style for text autolist -->
		<style type="text/css" media="screen">
			.form_tags { margin-bottom: 5px; }

			/* Setting widget width example */
			.textboxlist { width: 250px; }

			/* Preloader for autocomplete */
			.textboxlist-loading { background: url('images/spinner.gif') no-repeat 380px center; }

			/* Autocomplete results styling */
			.form_friends .textboxlist-autocomplete-result { overflow: hidden; zoom: 1; }
			.form_friends .textboxlist-autocomplete-result img { float: left; padding-right: 10px; }
			
		</style>

<form action="" method = "post">
	<table>
		<tr>
			<th>Tôi tên là: </th>
			<td class = "input"><input type = "text" name = "full_name"/></td>								
		</tr>
		<tr>
			<th>Giới tính: </th>
			<td class = "input">
				<select name = "gender">
					<option selected = "default" value = "-1">Tôi là...</option>
					<option value = "1">Nam</option>
					<option value = "0">Nữ</option>
				</select> 
			</td>
		</tr>
		<tr>
			<th>Sinh nhật: </th>
			<td>
				<select name = "day">
					<option selected = "default" value = "-1">Ngày: </option>
					<?php
					for($i = 1; $i<=31; $i++){
						echo '<option value = "'.$i.'">'.$i.'</option>';
					}
					?>
					
				</select>
				
				<select name = "month">
					<option selected = "default" value = "-1">Tháng: </option>
					<?php
					for($i = 1; $i<=12; $i++){
						echo '<option value = "'.$i.'">'.$i.'</option>';
					}
					?>
					
				</select>
				
				<select name = "year">
					<option selected = "default" value = "-1">Năm: </option>
					<?php
					$year = Date("Y");
					for($i = $year; $i>=1900; $i--){
						echo '<option value = "'.$i.'">'.$i.'</option>';
					}
					?>
					
				</select>
			</td>
			
		</tr>
		<tr>
			<th><a href = "#" title = "Nơi bạn đang học hoặc đã từng học.">Học trường?: </a></th>
			<td><input type = "text" name = "school" id = "form_tags_input_3"/></td>
		</tr>
		<tr><td></td></tr>
		<tr><td></td></tr>
		<tr>
			<td></td>
			<td class = "button"><input type = "submit" name = "submit" value = "Đăng kí"/></td>
		</tr>
	</table>
</form>
	<!-- required for TextboxList -->
		<script src="js/GrowingInput.js" type="text/javascript" charset="utf-8"></script>
				
		<script src="js/TextboxList.js" type="text/javascript" charset="utf-8"></script>		
		<script src="js/TextboxList.Autocomplete.js" type="text/javascript" charset="utf-8"></script>
		<!-- required for TextboxList.Autocomplete if method set to 'binary' -->
		<script src="js/TextboxList.Autocomplete.Binary.js" type="text/javascript" charset="utf-8"></script>		
		
		<!-- sample initialization -->

		<script type="text/javascript" charset="utf-8">		
			$(function(){
							
				// Autocomplete initialization
				var t4 = new $.TextboxList('#form_tags_input_3', {unique: true, plugins: {autocomplete: {}}});
							
				// sample data loading with json, but can be jsonp, local, etc. 
				// the only requirement is that you call setValues with an array of this format:
				// [
				//	[id, bit_plaintext (on which search is performed), bit_html (optional, otherwise plain_text is used), autocomplete_html (html for the item displayed in the autocomplete suggestions dropdown)]
				// ]
				// read autocomplete.php for a JSON response exmaple
				t4.getContainer().addClass('textboxlist-loading');				
				$.ajax({url: 'ajax/autocomplete-school.php', dataType: 'json', success: function(r){
					t4.plugins['autocomplete'].setValues(r);
					t4.getContainer().removeClass('textboxlist-loading');
				}});						
			});
		</script>
		
<?php
if($_POST['submit']){
	if($_POST['full_name'] && $_POST['gender'] != "-1"
	 && $_POST['day'] != "-1" && $_POST['month'] != "-1" && $_POST['year'] != "-1"){
	 	
		$full_name = $_POST['full_name'];
		$gender = $_POST['gender'];
		$birthday = $_POST['year']."-".$_POST['month']."-".$_POST['day'];
		$school = $_POST['school'];
		$date = date("Y-m-d");
		global $email;
		if(!preg_match('/[a-zA-Z]+/', $school)){
			//save name school
			$school_name = "";
			$select_school_id = DbSelect("SELECT `id`, `name` FROM `oGroups` WHERE `id` = '$school'");
			if($select_school_id){
				foreach($select_school_id as $values){
					$school_name = $values['name'];
				}										
				//query
				$query_insert = "INSERT INTO `oUsers`(
				`fullName`, `email`, `emailActive`, `birthday`, `gender`,
				`school`, `schoolId`, `registerDay`
				) 
				VALUES (
				'$full_name', '$email', '1', '$birthday', '$gender', '$school_name', '$school', '$date'
				)";
				
				//insert 
				$insert = DbInsert($query_insert);
				if($insert){
					echo "<p class = 'complete'>Đăng kí thành công. Vui lòng 
				<a href = 'login-openid.php'>đăng nhập</a> lại.</p>";		
				}
				else
					echo "<p class = 'error'>Có lỗi xảy ra, vui lòng quay lại sau.</p>";
			}
			else
				echo "<p class = 'error'>Có lỗi xảy ra, vui lòng quay lại sau.</p>";	
		}	
		//new school	
		else{
			//require random 
			require_once "include/random.php";
			//random id for groups										
			$school_id = RandNumber(6);
			
			//query isnsert users
			$query_insert = "INSERT INTO `oUsers`(
			`fullName`, `email`, `emailActive`, `birthday`, `gender`,
			`school`, `schoolId`, `registerDay`
			) 
			VALUES (
			'$full_name', '$email', '1', '$birthday', '$gender', '$school', '$school_id', '$date'
			)";
			//query insert groups
			$query_insert_g = "INSERT INTO `oGroups`(
				`id`, `name`, `status`
					) VALUES(
						'$school_id', '$school', '1')"
					;
			//insert 
			$insert = DbInsert($query_insert);
			$insert_group = DbInsert($query_insert_g);
			
			if($insert){
				echo "<p class = 'complete'>Đăng kí thành công. Vui lòng 
				<a href = 'login-openid.php'>đăng nhập</a> lại.</p>";
			}
			else if($insert_group){
				echo "<p class = 'error'>Đăng kí thất bại, vui lòng đăng kí lại.</p>";
			}
			else
				echo "<p class = 'error'>Đăng kí thất bại, vui lòng đăng kí lại.</p>";
		}						
	 }
	else
		echo "<p class = 'error'>Vui lòng nhập đầy đủ thông tin.</p>";
}

?>